package ivo.action;

import ivo.util.Base;

import java.io.IOException;

import javax.servlet.Filter;
import javax.servlet.FilterChain;
import javax.servlet.FilterConfig;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.lang.StringUtils;

public class LoginFilter implements Filter  {

	protected String encoding = null;    
    protected FilterConfig filterConfig;  
    /** 
     *  无参数构造方法
     */  
   
    public LoginFilter() {  
        // TODO Auto-generated constructor stub  
    }  
	@Override
	public void destroy() {
		// TODO Auto-generated method stub
		this.encoding=null;  
        this.filterConfig=null;  
	}

	//根据用户名和访问的标识来判断是否有权限来访问页面
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
			FilterChain chain) throws IOException, ServletException {
		// TODO Auto-generated method stub
		HttpServletRequest hRequest=(HttpServletRequest)request;  
        HttpServletResponse hResponse=(HttpServletResponse)response;
        String usr_id=hRequest.getParameter("usr_id");
        if( !StringUtils.isBlank(usr_id)){
            String usr_name=Base.decode(usr_id);
            String login_flg= hRequest.getParameter("login_flg");
            if(!StringUtils.isBlank(login_flg) && login_flg.equals("WQ==")){
            String login = Base.decode(login_flg);
            HttpSession session=hRequest.getSession();
            session.setAttribute("login_flg", login);
            session.setAttribute("usr", usr_name);
            chain.doFilter(hRequest, hResponse);
            return;
            }
            hResponse.sendRedirect("http://10.140.2.22:8080/mes-dx-web-login/home.jsp"); 
        }else{
        	hResponse.sendRedirect("http://10.140.2.22:8080/mes-dx-web-login");
        }
       
        }

	@Override
	public void init(FilterConfig filterConfig) throws ServletException {
		// TODO Auto-generated method stub
		this.filterConfig=filterConfig;  
        this.encoding=filterConfig.getInitParameter("encoding");  
        System.out.println(this.encoding);  
	}

}
